package com.example.demo.game;


import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketSession;

import java.util.concurrent.ConcurrentHashMap;

//房间管理器
@Component
@Slf4j
public class RoomManager {

    private ConcurrentHashMap<Long,Room> userIdToRoom = new ConcurrentHashMap<>();
    private ConcurrentHashMap<Long, WebSocketSession> gameRoom = new ConcurrentHashMap<>();


    public void remove(long userId1,long userId2){
        userIdToRoom.remove(userId1);
        userIdToRoom.remove(userId2);
    }

    public Room getRoomByUserId(long userId){
     Room room =  userIdToRoom.get(userId);
        if(room==null){
            return null;
        }
        return room;
    }
    public void put(Room room,Long userId){
        userIdToRoom.put(userId, room);
        System.out.println("此时userIdToRoom的个数为："+  userIdToRoom.size());
    }
    public void UserEnterGameRoom(long userId,WebSocketSession session){
        log.info("userId为："+userId+"的用户进入了房间");
        log.info("其session为："+session);
        gameRoom.put(userId,session);
        System.out.println("此时游戏房间的玩家个数为："+gameRoom.size());
    }
    public void UserRemoveGameRoom(Long userId){
        log.info("玩家userId为："+userId+"的用户出了游戏房间");
        gameRoom.remove(userId);
    }
    public WebSocketSession getUserSessionFromGameRoom(Long userId){
        log.info("获取user用户"+userId);
        System.out.println("此时的游戏房间的玩家有几个："+gameRoom.size());
        return gameRoom.get(userId);
    }
}
